package eyer.jvm;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.util.List;

public class JVMMemory implements JVMMemoryMBean {
    private static JVMMemory instance = new JVMMemory();
    private MemoryMXBean memoryMXBean;
    private MemoryPoolMXBean permGenMxBean;
    private MemoryPoolMXBean oldGenMxBean;
    private MemoryPoolMXBean edenSpaceMxBean;
    private MemoryPoolMXBean pSSurvivorSpaceMxBean;

    public static JVMMemory getInstance() {
        return instance;
    }

    private JVMMemory() {
        this.memoryMXBean = ManagementFactory.getMemoryMXBean();

        List<MemoryPoolMXBean> list = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean item : list) {
            if (("CMS Perm Gen".equals(item.getName())) || ("Perm Gen".equals(item.getName()))
                    || ("PS Perm Gen".equals(item.getName())) || ("G1 Perm Gen".equals(item.getName()))) {
                this.permGenMxBean = item;
            } else if (("CMS Old Gen".equals(item.getName())) || ("Tenured Gen".equals(item.getName()))
                    || ("PS Old Gen".equals(item.getName())) || ("G1 Old Gen".equals(item.getName()))) {
                this.oldGenMxBean = item;
            } else if (("Par Eden Space".equals(item.getName())) || ("Eden Space".equals(item.getName()))
                    || ("PS Eden Space".equals(item.getName())) || ("G1 Eden".equals(item.getName()))) {
                this.edenSpaceMxBean = item;
            } else if (("Par Survivor Space".equals(item.getName())) || ("Survivor Space".equals(item.getName()))
                    || ("PS Survivor Space".equals(item.getName())) || ("G1 Survivor".equals(item.getName()))) {
                this.pSSurvivorSpaceMxBean = item;
            }
        }
    }

    public long getHeapMemoryCommitted() {
        return this.memoryMXBean.getHeapMemoryUsage().getCommitted();
    }

    public long getHeapMemoryInit() {
        return this.memoryMXBean.getHeapMemoryUsage().getInit();
    }

    public long getHeapMemoryMax() {
        return this.memoryMXBean.getHeapMemoryUsage().getMax();
    }

    public long getHeapMemoryUsed() {
        return this.memoryMXBean.getHeapMemoryUsage().getUsed();
    }

    public long getNonHeapMemoryCommitted() {
        return this.memoryMXBean.getNonHeapMemoryUsage().getCommitted();
    }

    public long getNonHeapMemoryInit() {
        return this.memoryMXBean.getNonHeapMemoryUsage().getInit();
    }

    public long getNonHeapMemoryMax() {
        return this.memoryMXBean.getNonHeapMemoryUsage().getMax();
    }

    public long getNonHeapMemoryUsed() {
        return this.memoryMXBean.getNonHeapMemoryUsage().getUsed();
    }

    public long getPermGenCommitted() {
        if (null == this.permGenMxBean) {
            return 0L;
        }
        return this.permGenMxBean.getUsage().getCommitted();
    }

    public long getPermGenInit() {
        if (null == this.permGenMxBean) {
            return 0L;
        }
        return this.permGenMxBean.getUsage().getInit();
    }

    public long getPermGenMax() {
        if (null == this.permGenMxBean) {
            return 0L;
        }
        return this.permGenMxBean.getUsage().getMax();
    }

    public long getPermGenUsed() {
        if (null == this.permGenMxBean) {
            return 0L;
        }
        return this.permGenMxBean.getUsage().getUsed();
    }

    public long getOldGenCommitted() {
        if (null == this.oldGenMxBean) {
            return 0L;
        }
        return this.oldGenMxBean.getUsage().getCommitted();
    }

    public long getOldGenInit() {
        if (null == this.oldGenMxBean) {
            return 0L;
        }
        return this.oldGenMxBean.getUsage().getInit();
    }

    public long getOldGenMax() {
        if (null == this.oldGenMxBean) {
            return 0L;
        }
        return this.oldGenMxBean.getUsage().getMax();
    }

    public long getOldGenUsed() {
        if (null == this.oldGenMxBean) {
            return 0L;
        }
        return this.oldGenMxBean.getUsage().getUsed();
    }

    public long getEdenSpaceCommitted() {
        if (null == this.edenSpaceMxBean) {
            return 0L;
        }
        return this.edenSpaceMxBean.getUsage().getCommitted();
    }

    public long getEdenSpaceInit() {
        if (null == this.edenSpaceMxBean) {
            return 0L;
        }
        return this.edenSpaceMxBean.getUsage().getInit();
    }

    public long getEdenSpaceMax() {
        if (null == this.edenSpaceMxBean) {
            return 0L;
        }
        return this.edenSpaceMxBean.getUsage().getMax();
    }

    public long getEdenSpaceUsed() {
        if (null == this.edenSpaceMxBean) {
            return 0L;
        }
        return this.edenSpaceMxBean.getUsage().getUsed();
    }

    public long getSurvivorCommitted() {
        if (null == this.pSSurvivorSpaceMxBean) {
            return 0L;
        }
        return this.pSSurvivorSpaceMxBean.getUsage().getCommitted();
    }

    public long getSurvivorInit() {
        if (null == this.pSSurvivorSpaceMxBean) {
            return 0L;
        }
        return this.pSSurvivorSpaceMxBean.getUsage().getInit();
    }

    public long getSurvivorMax() {
        if (null == this.pSSurvivorSpaceMxBean) {
            return 0L;
        }
        return this.pSSurvivorSpaceMxBean.getUsage().getMax();
    }

    public long getSurvivorUsed() {
        if (null == this.pSSurvivorSpaceMxBean) {
            return 0L;
        }
        return this.pSSurvivorSpaceMxBean.getUsage().getUsed();
    }
}
